package com.lagou.demo01.servlet;

import com.lagou.demo01.entity.User;
import com.lagou.demo01.factory.UserDaoFactory;
import com.lagou.demo01.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求中的用户名和密码信息，并打印出来
        String userName = request.getParameter("userName");
        System.out.println("获取到的用户名是" + userName );
        String password = request.getParameter("password");
        System.out.println("获取到的密码是"+ password);
        //2.创建Userservice类型的对象去实现数据的校验功能
        UserService userService = new UserService();
        User user = userService.userLoginService(new User(userName, password));
        if (null == user) {
            System.out.println("登录失败，用户名或密码错误！");
        }else {
            System.out.println("登录成功，欢迎使用！");
            //重定向
            response.sendRedirect("manageStudent.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
